import 'package:flutter/material.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';

class LiveReceive extends StatelessWidget {
  IjkMediaController controller = IjkMediaController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('收流'),
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios),
            tooltip: 'Increase volume by 10',
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: ListView(
          children: <Widget>[
            Container(
              child: AspectRatio(
                aspectRatio: 3 / 4,
                child: IjkPlayer(
                  mediaController: controller,
                  controllerWidgetBuilder: (mediaCOntroller) {
                    return DefaultIJKControllerWidget(
                      showFullScreenButton: false,
                      playWillPauseOther: false,
                      horizontalGesture: false,
                      verticalGesture: false,
                      controller: mediaCOntroller,
                    );
                  },
                ),
              ),
            ),
            LiveReceiveController(controller: controller)
          ],
        ),
        // floatingActionButton: FloatingActionButton(
        //   child: Icon(Icons.play_arrow),
        //   onPressed: () async {
        //     await controller.setNetworkDataSource(
        //         'rtmp://192.168.5.44/live/flutter.flv',
        //         autoPlay: true);
        //     print("set data source success");
        //   },
        // ),
      ),
    );
  }
}

class LiveReceiveController extends StatefulWidget {
  final IjkMediaController controller;
  LiveReceiveController({Key key, this.controller}) : super(key: key);
  @override
  _LiveReceiveController createState() => _LiveReceiveController();
}

class _LiveReceiveController extends State<LiveReceiveController> {
  final urlHolder =
      TextEditingController(text: "rtmp://192.168.5.44/live/flutter.flv");

  @override
  void dispose() {
    this.widget.controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          TextField(
            controller: urlHolder,
            decoration: new InputDecoration(
              labelText: "url",
            ),
          ),
          makeButton(
            icon: this.widget.controller.isPlaying
                ? Icons.pause_circle_filled
                : Icons.play_circle_filled,
            text: this.widget.controller.isPlaying ? "暂停" : "播放",
            func: () async {
              print(urlHolder.text);
              await this
                  .widget
                  .controller
                  .setNetworkDataSource(urlHolder.text, autoPlay: true);
            },
          )
        ],
      ),
    );
  }

  Widget makeButton({IconData icon, String text, Function func}) {
    return Container(
      padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
      constraints: BoxConstraints(maxWidth: 170),
      // width: 150,
      height: 50,
      child: RaisedButton(
        child: Row(
          children: <Widget>[
            Icon(icon),
            Text(text),
          ],
        ),
        onPressed: () {
          func();
        },
      ),
    );
  }
}
